package org.byqh.dqeai.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.byqh.dqeai.common.Json;
import org.byqh.dqeai.entity.SoftInfo;
import org.byqh.dqeai.entity.proxy.SoftInfoProxy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/softInfo")
@Tag(name = "系统[SoftInfo] - 软件信息管理")
public class SoftInfoController
        extends BaseOpController<SoftInfo, SoftInfoProxy> {
    /**
     * 按CategoryId查询所有
     */
    @GetMapping("listByCategoryId")
    public Json<List<SoftInfo>> listByCategoryId(
            @RequestParam String categoryId) {
        return this.listImpl(SoftInfo.class, query -> {
            return query.where(o -> o.categoryId().eq(categoryId));
        });
    }

    /**
     * 按systemId和关键字查询所有
     */
    @GetMapping("listBySystemIdAndKeyword")
    public Json<List<SoftInfo>> listBySystemIdAndKeyword(
            @RequestParam String systemId,
            @RequestParam String keyword) {

        return this.listImpl(SoftInfo.class, query -> {
            String sql = "select category_id from SOFT_BUSINESS_CATEGORY where SYSTEM_ID = '" + systemId + "'";

            return query.where(o -> o.categoryId().in(easyEntityQuery.sqlQuery(sql, String.class)))
                    .where(s -> s.softName().like(keyword));
        });
    }

    /**
     * 按UserId查询所有
     */
    @GetMapping("listByUserId")
    public Json<List<SoftInfo>> listByUserId(
            @RequestParam String userId) {

        return this.listImpl(SoftInfo.class, query -> {
            String sql = "select soft_id from SOFT_FAVORITES where USER_ID = '" + userId + "'";

            return query.where(o -> o.softId().in(easyEntityQuery.sqlQuery(sql, String.class)));
        });
    }
}